package com.sicheng.lc.周赛.分类.模拟;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/3 10:36
 */
public class 解密消息 {
    //https://leetcode.cn/contest/weekly-contest-300/problems/decode-the-message/
    public String decodeMessage(String key, String message) {
        int[] map = new int[26];
        Arrays.fill(map, -1);
        int x = 0;
        for (int i = 0; x < map.length && i < key.length(); i++) {
            int ch = key.charAt(i) - 'a';
            if (ch >= 0 && ch <= 25 && map[ch] == -1) {
                map[ch] = x++;
            }
        }

        StringBuilder builder = new StringBuilder(message.length());
        for (int i = 0; i < message.length(); i++) {
            int ch = message.charAt(i) - 'a';
            if (message.charAt(i) != ' ')
                builder.append((char) (map[ch] + 'a'));
            else
                builder.append(' ');
        }

        return builder.toString();
    }

    public static void main(String[] args) {
        解密消息 s = new 解密消息();
        System.out.println(s.decodeMessage("the quick brown fox jumps over the lazy dog"
                , "vkbs bs t suepuv"));
    }
}
